<?php
class Pageley_Gateway_Database_TableMock implements Pageley_Gateway_Database_Table {
    
    private $entities;
    private $lastId = 0;
    private $schema;
    private $tableName;
    private $columnNames;
    
    public function __construct($schema = "\"dummy\"", $tableName = "\"dummy\"", $columnNames = null) {
        $this->entities = new Pageley_Util_ArrayMap();
        $this->schema = $schema;
        $this->tableName = $tableName;
        if($columnNames === null) {
            $columnNames = new Pageley_Util_ArrayMap();
            $columnNames->put("id", "\"dummy_id\"");
            $columnNames->put("name", "\"name\"");
            $columnNames->put("dummyType", "\"dummy_type\"");
        }
    }
    
    public function find($id) {
        $result = null;
    	foreach($this->entities as $entity) {
    		if($entity->getId() == $id) {
    			$result = $entity;
    			break;
    		}
    	}
    	return $result;
    }
	
	public function query($query) {
        $results = new Pageley_Util_ArrayMap();
        foreach($this->entities as $entity) {
            if($query->matches($entity)) {
                $results->add($entity);
            }
        }
        $order = $query->getOrder();
        if($order !== null) {
            $results->sort(new Pageley_Domain_EntityComparator($order));
        }
        $range = $query->getRange();
        if($range !== null) {
            $count = $results->size();
            $i = 0;
            while($i < $count) {
                if($i < $range->getStart() - 1 || $i > $range->getEnd() - 1) {
                    $results->remove($i);
                }
                $i++;
            }
        }
        return $results;
    }
	
	public function insert($entity) {
        $this->lastId++;
        $entity->setId($this->lastId);
    	$this->entities->add($entity);
    }
	
	public function update($entity) {
        
    }
	
	public function delete($entity) {
        $this->entities->removeValue($entity);
    }
    
    public function getColumnNames($prefix, $suffix = "") {
        $columnNames = "";
        foreach($this->columnNames as $columnName) {
            if($columnNames != "") {
                $columnNames .= ", ";
            }
            $columnNames .= $prefix . $columnName . $suffix;
        }
        return $columnNames;
    }
    
    public function getIdColumnName($prefix, $suffix = "") {
        return $prefix . $this->getTableName() . "_id" . $suffix;
    }
    
    public function getTableName() {
        return $this->tableName;
    }
    
    public function getSchema() {
        return $this->schema;
    }
    
    public function getFullTableName() {
        return $this->schema . "." . $this->tableName;
    }
    
    public function getMaxId() {
        return $this->lastId;
    }
    
    public function getColumnName($propertyName) {
        return $this->columnNames->get($propertyName);
    }
    
    public function count($query) {
        $count = 0;
         foreach($this->entities as $entity) {
            if($query->matches($entity)) {
                $count++;
            }
        }
        return $count;
    }
}